import * as userApi from '@/api/user.js'
import util from './utils.js'
import config from'@/config.js'
// 获取code
function getcode(){
	let _this = this
	let appid = uni.getStorageSync('appid')
	let local = encodeURIComponent(window.location.href);
	if(appid){
		window.location.href =
		"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+
		"&redirect_uri=" +
		local +
		"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
	}else{
		userApi.getAppid({
		}).then(res => {
			appid = res.data
			uni.setStorageSync('appid',res.data)
			window.location.href =
			"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+
			"&redirect_uri=" +
			local +
			"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
		})
	}
	
}
function wxLogin(){
	let _this = this
	let token = uni.getStorageSync("AccessToken")
	const oldCode = uni.getStorageSync('wechatCode')
	let code = util.checkgetUrlParam('code')
	let yq = util.checkgetYq()
	let type = util.checkGain()
		if(code == '' || code === null|| code == 'undefined' || code == oldCode){
			uni.setStorageSync('wechatCode', 0)
			getcode()
		}else{
			userApi.authLogin({
				code,
				yq
			}).then(res => {
				if(res.code === 1){
					uni.setStorageSync("AccessToken",res.data.token)
					uni.setStorageSync('wechatCode', code)
					window.location.reload();
					return true
				}else{
					uni.setStorageSync('wechatCode', code)
					return false
				}
			})
		}
}
module.exports = {
	wxLogin:wxLogin
}